import java.util.List;

/**
 * Created by sunchenglin on 2017/7/2. 4
 在一个排序的链表中，存在重复的结点，请删除该链表中重复的结点，重复的结点不保留，返回链表头指针。
 例如，链表1->2->3->3->4->4->5 处理后为 1->2->5

 思路：
 （1）遍历存储重复节点，再遍历删除重复节点。 数组定义多大呢。。不好。
 （2）三个指针，
 一个指向当前节点，
 一个指向当前节点的前面，
 另一个指向当前节点的后面，看是否重复，
 */
public class Solution {

    public ListNode deleteDuplication(ListNode pHead)
    {
        int temp;
        ListNode first = pHead;
        ListNode now_before = null;
        ListNode now_after;
        while(pHead!=null){
            temp = pHead.val;
            now_after = pHead.next;
            while(now_after!=null && temp == now_after.val){
                    now_after = now_after.next;
            }
            if (pHead.next != now_after){
                if(now_before==null){
                    first = now_after;
                }
                else{
                    now_before.next = now_after;
                }
            }else{
                now_before = pHead;
            }
            pHead = now_after;
        }
        return first;
    }


    public static void main(String args[]){

        Solution s = new Solution();
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(2);
        ListNode node4 = new ListNode(3);
        ListNode node5 = new ListNode(4);
        ListNode node6 = new ListNode(5);
        ListNode node7 = new ListNode(5);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;
        node6.next = node7;

        System.out.println(s.deleteDuplication(node1));
        System.out.print("haha");
    }
}

class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}

